<?php
!defined('IN_FENGTOPsso') && exit('Access Denied');

class Register extends Base{
	public  $msg;
	private $userName;
	private $userPwd1;
	private $userPwd2;
	private $userMail;
	private $regdate;
	
	public function __construct() {
		parent::__construct();
		$this->userName = daddslashes($_POST['userName']);
		$this->userPwd1 = daddslashes($_POST['userPwd1']);
		$this->userPwd2 = daddslashes($_POST['userPwd2']);
		$this->userMail = daddslashes($_POST['userMail']);
		$this->msg      = "0";
		$this->regdate  = time();
//echo $this->userName."    ".$this->userPwd1."    ".$this->userPwd2;
	}
	
	public function checkIn() {
		//两次输入的密码不一致或者没有输入密码
		if(($this->userPwd1 != $this->userPwd2) || ($this->userPwd1 == ''))
		{
			$this->msg = "1";
		}
		//检查用户是否存在
		if($this->checkUserName() != 0){
			$this->msg = '2'; //用户已存在
		}
	}
	
	public function writeInSQL() {
		if($this->msg == "0"){
			//插入user表数据
			$insertSQLuser = "INSERT INTO `".$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."user_".substr(md5($this->userName), 0, 2)."` ( 
			`uid` ,
			`username` ,
			`password` ,
			`email` ,
			`user_sex` ,
			`status` ,
			`emailstatus` ,
			`regdate` ,
			`regip` ,
			`lastlogintime` ,
			`lastloginip` ,
			`question` ,
			`uanswer` )
			VALUES ( NULL,
			'".$this->userName."',
			'".md5($this->userPwd1)."',
			'".$this->userMail."',
			'0',
			'1',
			'0',
			'".$this->regdate."',
			'".$_SERVER['REMOTE_ADDR']."',
			'1',
			'1',
			'1',
			'1');" ;
			$resultUser = $this->mysqldb->Query($insertSQLuser);
			//插入user-mail表数据
			$insertSQLusermail = "INSERT INTO `".$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."usermail_".substr(md5($this->userMail), 0, 2)."` ( 
			`email` ,
			`username` )
			VALUES ( '".$this->userMail."','".$this->userName."');" ;
			$resultUserMail = $this->mysqldb->Query($insertSQLusermail);
			if($resultUser && $resultUserMail){
				$this->msg = "0"; //成功
				$this->sendMail();
			}
			else {
				$this->msg = "2";
			}
		}
	}
	
	private function sendMail(){
		require_once FENGTOPsso_DIR.'class/class.phpmailer.php';

		$mail = new PHPMailer();
		$mail->IsSMTP();					// 启用SMTP
		
		//$mail->Host = "61.136.58.211";			//SMTP服务器
		//$mail->SMTPAuth = true;					//开启SMTP认证
		//$mail->Username = "fengtop@fengtop.net";	// SMTP用户名
		//$mail->Password = "123456789";			// SMTP密码
		$mail->Host = $GLOBALS['_FENGTOPsso']['smtp']['url'];			//SMTP服务器
		$mail->SMTPAuth = true;											//开启SMTP认证
		$mail->Username = $GLOBALS['_FENGTOPsso']['smtp']['user'];		// SMTP用户名
		$mail->Password = $GLOBALS['_FENGTOPsso']['smtp']['pwd'];		// SMTP密码
		
		$mail->From = $GLOBALS['_FENGTOPsso']['smtp']['user'];			//发件人地址
		$mail->FromName = $GLOBALS['_FENGTOPsso']['smtp']['username'];	//发件人
		
		//$mail->AddAddress("305412221@qq.com", "feng");	//添加收件人
		//$mail->AddAddress("fengtop@hotmail.com");
		$mail->AddAddress($this->userMail);  
		//$mail->AddReplyTo("fengtop@163.com", "Information");	//回复地址
		$mail->WordWrap = 50;					//设置每行字符长度
		/** 附件设置
		$mail->AddAttachment("/var/tmp/file.tar.gz");		// 添加附件
		$mail->AddAttachment("/tmp/image.jpg", "new.jpg");	// 添加附件,并指定名称
		*/
		$mail->IsHTML(true);					// 是否HTML格式邮件
		$mail->CharSet = "utf-8";				// 这里指定字符集！
		$mail->Encoding = "base64"; 
		
		$attestationkey = md5($this->regdate.$_SERVER['REMOTE_ADDR'].$this->userName);
		$mail->Subject = "fengtop用户注册激活";			//邮件主题
		$mail->Body    = '<html><head>
		<meta http-equiv="Content-Language" content="zh-cn">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
		<body><td colspan="2" style="padding: 10px 0px 0pt 30px; margin: 0pt;">
		    <p style="margin: 0pt; line-height: 25px;">亲爱的<a style="color: rgb(69, 154, 214); text-decoration: none;" title="Tieowr">'.$this->userName.'</a>：</p>
		    <p style="padding: 0pt 20px; margin: 0pt; line-height: 25px;">离成功注册FENGTOP帐号就差一步了！</p>
		    <p style="padding: 0pt 20px; margin: 0pt; line-height: 25px;">您的FENGTOP帐号为： <span style="color: rgb(240, 131, 0);"><a target="_blank" href="mailto:'.$this->userMail.'">'.$this->userName.'</a></span></p>

		    <p style="padding: 20px 20px 5px; margin: 0pt; color: rgb(69, 154, 214);">点击以下链接完成激活,或把下面网页地址复制到浏览器的地址栏中打开:</p>
		    <p style="padding: 0pt 20px; margin: 0pt; color: rgb(69, 154, 214);"><a href="http://fengtop.net/index.php?action=registerActive&key='.$attestationkey.'&amp;mail='.$this->userMail.'" target="_blank" title="点击完成注册" style="margin: 0pt; color: rgb(69, 154, 214);">http://fengtop.net/index.php?action=registerActive&key='.$attestationkey.'&amp;mail='.$this->userMail.'</a></p>
		    <p style="padding: 30px 20px 0pt; margin: 0pt; color: rgb(150, 150, 150); font-size: 12px;">温馨提示:</p>
		    <p style="padding: 0pt 20px; margin: 0pt; color: rgb(150, 150, 150); font-size: 12px;">新注册帐号在激活24小时内开通迅雷会员即可获得同等时长价值3元的1G离线空间</p>
		    <p style="padding: 0pt 20px;"><a href="http://fengtop.net/index.html?referfrom=ZC_51" target="_blank" title="立即开通"><img style="border: 0pt none;" src="http://fengtop.net/v2.5/img/mail/kt.png"></a></p>
		    <p style="padding: 80px 0pt 0pt 20px; margin: 0pt 0pt 0pt -30px;"><img src="http://fengtop.net/v2.5/img/mail/i.png"></p>
		    <p style="padding: 5px 20px 0pt; margin: 0pt 0pt 0pt -25px; font-size: 12px; line-height: 20px; color: rgb(180, 180, 180);">您收到此邮件是因为您在FENGTOP注册并同意接受来自该网站的邮件。</p>
		    <p style="padding: 0pt 20px; margin: 0pt 0pt 0pt -25px; font-size: 12px; line-height: 20px; color: rgb(180, 180, 180);">请保管好您的邮箱，避免FENGTOP帐号被他人盗用</p>
		    <p style="padding: 0pt 20px; margin: 0pt 0pt 0pt -25px; font-size: 12px; line-height: 20px; color: rgb(180, 180, 180);">如有任何疑问，请访问<a href="http://fengtop.net/help/" target="_blank" title="http://fengtop.net/help/" style="color: rgb(180, 180, 180); text-decoration: none;">http://fengtop.net/help/</a></p></td></body> </html> ';  //邮件内容
		$mail->AltBody = "This is the body in plain text for non-HTML mail clients";	//邮件正文不支持HTML的备用显示
		
		if(!$mail->Send())
		{
			echo '<html><head>
			      <meta http-equiv="Content-Language" content="zh-cn">
			      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
			      <body>';
			echo "消息未能正确发送. <p>";
			echo "发送方错误信息: " . $mail->ErrorInfo;
			echo '</body> </html> ';
			exit;
		}else {
			//echo "Message has been sent";
			$this->setAttestation($attestationkey);
		}
	}
	
	private function setAttestation($attestationkey){
		$nowTime = time();
		$insertMailAttestation = "INSERT INTO `{$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']}mail_attestation` ( 
		                          `username`, `email`, `attestationkey`, `regdate`)
		                          VALUES ('{$this->userName}', '{$this->userMail}', '{$attestationkey}', '{$nowTime}')";
//echo $insertMailAttestation;
		$this->mysqldb->Query($insertMailAttestation);
	}
	
	private function checkUserName(){
		$getUserName = 'select username from `'.$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."user_".substr(md5($this->userName), 0, 2).'`
		                 where username = \''.$this->userName.'\'';
		$result = $this->mysqldb->GetOne($getUserName);
		return $result;
	}
	
//	function __destruct() {
//		parent::__destruct();
//	}
}
?>